package com.module.base.widget.skin.s

import android.content.Context
import android.util.AttributeSet
import androidx.annotation.DrawableRes
import com.hjq.shape.styleable.*
import com.hjq.shape.view.ShapeRadioButton
import skin.support.widget.SkinCompatSupportable

/**
 * Author: yanxuwen
 * Date: 2023/4/23
 * Desc: ShapeRadioButton 皮肤处理
 *       系统RadioButton 皮肤处理(抄SkinCompatRadioButton)

 */
class SRadioButton : ShapeRadioButton, SkinCompatSupportable {

    private fun getStyleable(): IShapeDrawableStyleable {
        return ShapeRadioButtonStyleable()
    }

    private fun getTextStyleable(): ITextColorStyleable {
        return ShapeRadioButtonStyleable()
    }

    private fun getCompoundStyleable(): ICompoundButtonStyleable? {
        return ShapeRadioButtonStyleable()
    }

    lateinit var impl: SkinShapeImpl
    lateinit var implCompat: SkinCompatImpl

    constructor(context: Context) : super(context) {
        initSkin(null, 0)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initSkin(attrs, 0)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initSkin(attrs, defStyleAttr)
    }

    private fun initSkin(attrs: AttributeSet?, defStyleAttr: Int) {
        if (isInEditMode) {
            return
        }
        impl = SkinShapeImpl(
            context,
            shapeDrawableBuilder,
            getStyleable(),
            textColorBuilder,
            getTextStyleable(),
            buttonDrawableBuilder,
            getCompoundStyleable()
        )
        impl.initTypedArray(this, attrs)
        impl.applyBorderColorResource()

        implCompat = SkinCompatImpl(this)
        implCompat.initTypedArray(attrs, defStyleAttr)
    }


    override fun setButtonDrawable(@DrawableRes resId: Int) {
        super.setButtonDrawable(resId)
        implCompat.setButtonDrawable(resId)
    }

    override fun setTextAppearance(resId: Int) {
        setTextAppearance(context, resId)
    }

    override fun setTextAppearance(context: Context?, resId: Int) {
        super.setTextAppearance(context, resId)
        implCompat.setTextAppearance(context, resId)
    }

    override fun setCompoundDrawablesRelativeWithIntrinsicBounds(
        @DrawableRes start: Int, @DrawableRes top: Int, @DrawableRes end: Int, @DrawableRes bottom: Int
    ) {
        super.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)
        implCompat.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)
    }

    override fun setCompoundDrawablesWithIntrinsicBounds(
        @DrawableRes left: Int, @DrawableRes top: Int, @DrawableRes right: Int, @DrawableRes bottom: Int
    ) {
        super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
        implCompat.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
    }

    override fun setBackgroundResource(resId: Int) {
        super.setBackgroundResource(resId)
        implCompat.setBackgroundResource(resId)
    }


    override fun applySkin() {
        impl.applySkin()
        implCompat.applySkin()
    }
}

